package org.vectrics.skill.struts;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

import org.apache.ecs.html.A;
import org.apache.log4j.Logger;
import org.vectrics.DataException;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Form;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.common.pagelist.PageList;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.skill.UserEvaluation;
import org.vectrics.struts.PageListTag;
import org.vectrics.struts.Request;
import org.vectrics.user.Person;

/**
 * @author mmoore
 * 
 *         Shows a list of job postings. This list can take different forms and
 *         can include different categories of jobs. If 'observer' property is
 *         'true', the job list will contain just jobs that are of concern to
 *         the current authentication user.
 * 
 */
public class UserEvaluationListTag extends PageListTag {

    private static final long serialVersionUID = -4708220244555085385L;

    static private transient Logger log = Logger.getLogger(UserEvaluationListTag.class);

    static private SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");

    static public String USER_PARAM = "user";

    static public String PAGE_DESCRIPTION = null;

    public static String TABLE_FONT = "subFont";

    private String userId = null;

    public static String NO_ELEMENTS_MESSAGE = "No Evaluations Found";

    @Override
    protected String getTopOfPageContent() {
        String content = "";
        return content;
    }

    @Override
    protected PageList createPageList() {
        Person person = CoreServiceLocator.getPersonService().findPersonById(new Long(userId));
        UserEvaluationPageList pageList = new UserEvaluationPageList(person);
        return pageList;
    }

    /**
     * @return Returns the userId.
     */
    public String getUserId() {
        return userId;
    }

    /**
     * @param userId
     *            The userId to set.
     */
    public void setUserId(final String userId) {
        this.userId = userId;
        if (this.userId != null && userId.equals("current")) {
            this.userId = ThreadProperties.getPerson().getId().toString();
        }
    }

    @Override
    protected boolean usePageListing() {
        boolean result = false;
        return result;
    }

    @Override
    protected String getNoElementsMessage() {
        return NO_ELEMENTS_MESSAGE;
    }

    @Override
    protected String getDescription() {
        return PAGE_DESCRIPTION;
    }

    @Override
    protected String getPageListLink() {
        return getContextPath() + "/admin/user/eval/list.do";
    }

    @Override
    protected void showList(final String str, final Cell listCell, final List evals, final Form form)
            throws DataException {
        if (log.isDebugEnabled()) {
            log.debug("showList() called");
            log.debug("showList() evals.size = " + evals.size());
        }

        Person user = null;
        try {
            user = getAuthPerson();
        } catch (Exception ex) {
            log.error("Exception finding auth user", ex);
            throw new DataException("Exception finding auth user", ex);
        }

        if (evals.size() > 0) {
            Iterator iter = evals.iterator();
            BorderTable listTable = new BorderTable();
            listTable.setWidth("100%");
            listCell.addElement(listTable);
            Row hdrRow = listTable.addRow();
            hdrRow.setDefaultBgImage(getContextPath() + "/site-graphics/tbl_hdr.gif");
            Cell hdrCell = hdrRow.addCell();

            hdrCell.addElement(TextHelper.getFont("tableHdrFont", "Date"));

            hdrCell = hdrRow.addCell();
            hdrCell.setWidth(80);
            hdrCell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.evaluator")));
            // hdrCell.setWidth(220);

            hdrCell = hdrRow.addCell();
            hdrCell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.evaluation.description")));

            hdrCell = hdrRow.addCell();
            hdrCell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.evaluation.score")));

            hdrCell = hdrRow.addCell();
            hdrCell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.evaluation.type")));

            if (hasPermission("manage_candidate_items")) {
                hdrCell = hdrRow.addCell();
                hdrCell.addElement("&nbsp;");
            }

            while (iter.hasNext()) {
                UserEvaluation eval = (UserEvaluation) iter.next();

                addEvaluationRow(eval, listTable, getRequest(), user);
            }
        }
    }

    void addEvaluationRow(final UserEvaluation eval, final Table listTable, final Request request, final Person authUser) {
        log.debug("addJobRow() - method to display passed job");
        Row row = listTable.addRow();
        Cell cell = row.addCell();

        String dateStr = dateFormatter.format(eval.getDateStart());
        cell.addElement(TextHelper.getFont(TABLE_FONT, dateStr));

        cell = row.addCell();
        String evaluatorName = "Not Specified";
        if (eval.getLeadPerson() != null) {
            Person evaluator = eval.getLeadPerson();
            evaluatorName = evaluator.getName();
        }
        cell.addElement(TextHelper.getFont(TABLE_FONT, evaluatorName));

        cell = row.addCell();
        cell.addElement(TextHelper.getFont(TABLE_FONT, eval.getDescription()));

        cell = row.addCell();
        cell.addElement(TextHelper.getFont(TABLE_FONT, eval.getScore().toString()));

        cell = row.addCell();
        cell.addElement(TextHelper.getFont(TABLE_FONT, eval.getType().getDescription()));

        if (hasPermission("manage_candidate_items")) {
            cell = row.addCell();
            A editLink = new A();
            editLink.setClass("lnkSubHover");
            editLink.setHref(request.getContextPath() + "/admin/user/eval/view.do" + "?evaluationId=" + eval.getId()
                    + "&userId=" + eval.getPerson().getId());
            editLink.addElement(this.getPhrase("phrase.view"));
            cell.addElement(editLink);
        }

    }

}
